<!--/**
* @author xuyh0817
* @email xuyh0817@foxmail.com
* @date 2022-02-16 15:58
* @version 2.0.0
* @doc todo
* @website todo
* @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
*/
-->
<template>
  <a-rate
    v-if="!!isIcon"
    :value="currentValue"
    v-bind="attrs"
    :tooltips="tooltips"
    @change="changeHandler"
  >
    <a-icon
      slot="character"
      v-bind="iconAttrs"
      :type="icon && !(slots.hasOwnProperty('character'))"
      :style="iconStyle"
    />
<!--    <extend-slot v-for="(render, key) of slots" v-slot:[key] :key="key" :data="slotData" :render="render"/>-->
  </a-rate>

  <a-rate
    v-else
    :value="currentValue"
    v-bind="attrs"
    :tooltips="tooltips"
    :character="icon"
    :style="iconStyle"
    @change="changeHandler"
  >
<!--    <extend-slot v-for="(render, key) of slots" v-slot:[key] :key="key" :data="slotData" :render="render"/>-->
  </a-rate>
</template>

<script>
import {COMPONENT_TYPE, CUSTOM_EVENT_TYPE} from '../../constants'
import ComponentMixin from '../../XRenderMixins/XRenderComponentMixin'

export default {
  name: COMPONENT_TYPE.RATE,
  mixins: [ComponentMixin],
  props: {
    value: {
      type: Number
    }
  },
  computed: {
    currentValue: {
      get() {
        const {value} = this
        return value ? (typeof (value) === 'number' ? value : 0) : 0
      }
    },
    icon: {
      get() {
        const {isIcon, iconAttrs} = this
        const {type} = iconAttrs
        if (isIcon) {
          return type.substr(5)
        }
        return type
      }
    },
    iconAttrs: {
      get() {
        const {attrs} = this
        const {icon} = attrs
        return icon
      }
    },
    isIcon: {
      get() {
        const {iconAttrs} = this
        const {type} = iconAttrs
        return !!type && type.startsWith('icon-')
      }
    },
    iconStyle: {
      get() {
        const {iconAttrs, formItem, isDebug} = this
        const {style} = iconAttrs
        if (typeof (style) !== 'string' && typeof (style) !== 'object') {
          isDebug ? console.warn(`${formItem.prop} icon.style类型错误,非String或Object`) : ''
          return {}
        }
        if (typeof (style) === 'object') {
          return style
        }
        return (new Function('return ' + style))()
      }
    },
    tooltips: {
      get() {
        const {attrs, formItem, isDebug} = this
        const {tooltips} = attrs
        if (typeof (tooltips) !== 'string' && !(tooltips instanceof Array)) {
          isDebug ? console.warn(`${formItem.prop} tooltips类型错误,非String或Array`) : ''
          return []
        }
        if (tooltips instanceof Array) {
          return tooltips || []
        }
        return (new Function('return ' + tooltips))()
      }
    }
  },
  methods: {
    changeHandler(value) {
      const {formItem, attrs} = this
      this.$emit(CUSTOM_EVENT_TYPE.CHANGE, {
        type: COMPONENT_TYPE.RATE,
        prop: formItem ? formItem.prop : '',
        value,
        attrs,
        formItem
      })
    }
  }
}
</script>

<style scoped>

</style>
